# -*- coding: UTF-8 -*-

import hashlib
import base64
import random
import string
import uuid

__doc__ = """
    For example:
        print(get_sha256("123"))----->a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
        print(get_sha256l(["1", "2", "3"]))------>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
        print(get_safe_b64("123"))------>b'MTIz'
        print(safe_b64decode(b'MTIz'))------>123
"""


# base解密
def safe_b64decode(s):
    length = len(s) % 4
    for i in range(length):
        s = s + '='
    return bytes.decode((base64.urlsafe_b64decode(s)))


# 加密
def get_safe_b64(s):
    return base64.urlsafe_b64encode(bytes(s, encoding='utf8'))


def get_sha256(str_):
    return hashlib.sha256(str_.encode("utf-8")).hexdigest()


def get_md5(str_):
    return hashlib.md5(str_.encode("utf-8")).hexdigest()


def get_sha256l(list_):
    m = hashlib.sha256()
    for i in list_:
        m.update(i.encode("utf-8"))
    return m.hexdigest()


def get_code(position: int = 5):
    """
    X位数的随机码 默认5位
    """
    position = -abs(position) if abs(position) < 30 else -5
    return str(uuid.uuid4()).replace("-", "0")[position::]


def get_uuid():
    return str(uuid.uuid4())


def create_string_number(n):
    """生成一串指定位数的字符+数组混合的字符串"""
    m = random.randint(1, n)
    a = "".join([str(random.randint(0, 9)) for _ in range(m)])
    b = "".join([random.choice(string.ascii_letters) for _ in range(n - m)])
    return ''.join(random.sample(list(a + b), n))


if __name__ == '__main__':
    print(get_uuid())
